package com.benjaminwan.read.free.config

import com.benjaminwan.read.free.common.KeyDefinition
import com.benjaminwan.read.free.config.Options.getAction
import com.benjaminwan.read.free.model.ItemConfig
import com.benjaminwan.read.free.model.enums.ItemType
import com.benjaminwan.read.free.model.action.KeyboardKeyUp
import com.benjaminwan.read.free.model.action.Position
import com.benjaminwan.read.free.model.listOfItemConfig
import com.benjaminwan.read.free.ui.main.MainViewModel
import com.benjaminwan.read.free.utils.getRegexFilter
import com.benjaminwan.read.free.utils.numRegex
import com.benjaminwan.read.free.utils.showPicker
import com.benjaminwan.read.free.utils.showRightTextInputDialog
import com.xuexiang.xui.widget.textview.supertextview.SuperTextView

object KeyboardKeyUpConfig {
    fun getKeyboardKeyUpConfig(
        mainVM: MainViewModel, pos: Position
    ): List<ItemConfig> {
        return listOfItemConfig {
            itemConfig {
                type = ItemType.Text
                titleStr = "键值(手工输入)"
                rightTextFunc = { getAction<KeyboardKeyUp>(mainVM, pos)?.key?.toString() ?: "" }
                click = { view ->
                    (view as SuperTextView).showRightTextInputDialog(
                        title, 1, 3, getRegexFilter(numRegex), { dialog, which ->
                            val default = ""
                            dialog.inputEditText?.setText(default)
                            dialog.inputEditText?.setSelection(default.length)
                        }) { dialog, which ->
                        val input = dialog.inputEditText?.text.toString()
                        val action =
                            getAction<KeyboardKeyUp>(mainVM, pos) ?: return@showRightTextInputDialog
                        val key = input.toInt()
                        mainVM.setAction(pos, action.copy(key = key))
                        dialog.dismiss()
                    }
                }
            }
            itemConfig {
                type = ItemType.Text
                titleStr = "QWERTY区按键选择"
                rightTextFunc = {
                    val key = getAction<KeyboardKeyUp>(mainVM, pos)?.key
                    if (key == null) {
                        "没有设置键值"
                    } else {
                        KeyDefinition.findKeyName(key, KeyDefinition.QWERTY_DEFINITION)
                    }
                }
                click = { view ->
                    (view as SuperTextView).showPicker(
                        title, KeyDefinition.QWERTY_DEFINITION
                    ) { opValue, opName ->
                        if (opName == view.rightString) return@showPicker
                        val action = getAction<KeyboardKeyUp>(mainVM, pos) ?: return@showPicker
                        mainVM.setAction(pos, action.copy(key = opValue))
                    }
                }
            }
            itemConfig {
                type = ItemType.Text
                titleStr = "Navigation区按键选择"
                rightTextFunc = {
                    val key = getAction<KeyboardKeyUp>(mainVM, pos)?.key
                    if (key == null) {
                        "没有设置键值"
                    } else {
                        KeyDefinition.findKeyName(key, KeyDefinition.NAVIGATION_DEFINITION)
                    }
                }
                click = { view ->
                    (view as SuperTextView).showPicker(
                        title,
                        KeyDefinition.NAVIGATION_DEFINITION
                    ) { opValue, opName ->
                        if (opName == view.rightString) return@showPicker
                        val action = getAction<KeyboardKeyUp>(mainVM, pos) ?: return@showPicker
                        mainVM.setAction(pos, action.copy(key = opValue))
                    }
                }
            }
            itemConfig {
                type = ItemType.Text
                titleStr = "Numeric区按键选择"
                rightTextFunc = {
                    val key = getAction<KeyboardKeyUp>(mainVM, pos)?.key
                    if (key == null) {
                        "没有设置键值"
                    } else {
                        KeyDefinition.findKeyName(key, KeyDefinition.NUMERIC_DEFINITION)
                    }
                }
                click = { view ->
                    (view as SuperTextView).showPicker(
                        title,
                        KeyDefinition.NUMERIC_DEFINITION
                    ) { opValue, opName ->
                        if (opName == view.rightString) return@showPicker
                        val action = getAction<KeyboardKeyUp>(mainVM, pos) ?: return@showPicker
                        mainVM.setAction(pos, action.copy(key = opValue))
                    }
                }
            }

        }
    }
}